#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
#define endl '\n'
using namespace std;
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	vector<int> a(n + 1, 0);
	vector<int> pre(n + 1, 0);
	unordered_map<int, int> pos;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(a[i] == 0) a[i] = -1;
	}
	pos[0] = 0;
	int maxn = 0;
	for(int i = 1; i <= n; i++){
		pre[i] = pre[i - 1] + a[i];
		if(pos.count(pre[i])) maxn = max(maxn, i - pos[pre[i]]);
		else pos[pre[i]] = i;
	}
	cout << maxn << endl;
	return 0;
}